﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="check_by_time.aspx.cs" Inherits="SQT.tracnghiem.Ajax.ASP.Accountant.check_by_time" %>

<%@ Import Namespace="DataAccess" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <thead>
        <tr class="w3-teal">
            <th>Mã NV</th>
            <th>Họ tên</th>
            <th>Ngày làm</th>
            <th>Chi tiết giờ làm</th>
            <th>Tổng số giờ</th>
        </tr>
    </thead>
    <%
        DateTime from = DateTime.Parse(Request.QueryString["date_from"]);
        DateTime to = DateTime.Parse(Request.QueryString["date_to"]);
        int staff = int.Parse(Request.QueryString["staff"]);
        if (staff == 0)
        {
            foreach (TblCheckManager chk in new TblCheckManagerController().FetchAll().Where(x => DateTime.Parse(x.CheckIn.ToString()).Date >= from.Date && DateTime.Parse(x.CheckIn.ToString()).Date <= to.Date && x.Accept==true))
            {
                var get_admin = new TblManagerController().FetchAll().Where(x => x.ManagerId == chk.ManagerId).Single();
                string checkout = "";
                float hour = 0, minute = 0;
                if (chk.CheckOut != null)
                {
                    checkout = DateTime.Parse(chk.CheckOut.ToString()).Hour.ToString() + ":" + DateTime.Parse(chk.CheckOut.ToString()).Minute.ToString();
                    if (DateTime.Parse(chk.CheckOut.ToString()).Hour >= DateTime.Parse(chk.CheckIn.ToString()).Hour)
                    {
                        hour = DateTime.Parse(chk.CheckOut.ToString()).Hour - DateTime.Parse(chk.CheckIn.ToString()).Hour;
                    }
                    else
                    {
                        hour = 24 - DateTime.Parse(chk.CheckIn.ToString()).Hour + DateTime.Parse(chk.CheckOut.ToString()).Hour;
                    }

                    if (DateTime.Parse(chk.CheckIn.ToString()).Minute <= DateTime.Parse(chk.CheckOut.ToString()).Minute)
                    {
                        minute = DateTime.Parse(chk.CheckOut.ToString()).Minute - DateTime.Parse(chk.CheckIn.ToString()).Minute;
                    }
                    else
                    {
                        minute = 60 - DateTime.Parse(chk.CheckIn.ToString()).Minute + DateTime.Parse(chk.CheckOut.ToString()).Minute;
                        hour--;
                    }
                }
                              
    %>
    <tr>
        <td><%=get_admin.ManagerId%></td>
        <td><%=get_admin.FullName%></td>
        <td><%=DateTime.Parse(chk.CheckIn.ToString()).Day%>/<%=DateTime.Parse(chk.CheckIn.ToString()).Month%>/<%=DateTime.Parse(chk.CheckIn.ToString()).Year%></td>
        <td><%=DateTime.Parse(chk.CheckIn.ToString()).Hour%>:<%=DateTime.Parse(chk.CheckIn.ToString()).Minute%> - <%=checkout%></td>
        <td><%=hour%> giờ <%=minute%> phút</td>
    </tr>
    <%}
        }
        else
        {
            foreach (TblCheckManager chk in new TblCheckManagerController().FetchAll().Where(x => DateTime.Parse(x.CheckIn.ToString()).Date >= from.Date && DateTime.Parse(x.CheckIn.ToString()).Date <= to.Date && x.ManagerId == staff && x.Accept==true))
            {
                var get_admin = new TblManagerController().FetchAll().Where(x => x.ManagerId == chk.ManagerId).Single();
                string checkout = "";
                float hour = 0, minute = 0;
                if (chk.CheckOut != null)
                {
                    checkout = DateTime.Parse(chk.CheckOut.ToString()).Hour.ToString() + ":" + DateTime.Parse(chk.CheckOut.ToString()).Minute.ToString();
                    if (DateTime.Parse(chk.CheckOut.ToString()).Hour >= DateTime.Parse(chk.CheckIn.ToString()).Hour)
                    {
                        hour = DateTime.Parse(chk.CheckOut.ToString()).Hour - DateTime.Parse(chk.CheckIn.ToString()).Hour;
                    }
                    else
                    {
                        hour = 24 - DateTime.Parse(chk.CheckIn.ToString()).Hour + DateTime.Parse(chk.CheckOut.ToString()).Hour;
                    }

                    if (DateTime.Parse(chk.CheckIn.ToString()).Minute <= DateTime.Parse(chk.CheckOut.ToString()).Minute)
                    {
                        minute = DateTime.Parse(chk.CheckOut.ToString()).Minute - DateTime.Parse(chk.CheckIn.ToString()).Minute;
                    }
                    else
                    {
                        minute = 60 - DateTime.Parse(chk.CheckIn.ToString()).Minute + DateTime.Parse(chk.CheckOut.ToString()).Minute;
                        hour--;
                    }
                }
                              
    %>
    <tr>
        <td><%=get_admin.ManagerId%></td>
        <td><%=get_admin.FullName%></td>
        <td><%=DateTime.Parse(chk.CheckIn.ToString()).Day%>/<%=DateTime.Parse(chk.CheckIn.ToString()).Month%>/<%=DateTime.Parse(chk.CheckIn.ToString()).Year%></td>
        <td><%=DateTime.Parse(chk.CheckIn.ToString()).Hour%>:<%=DateTime.Parse(chk.CheckIn.ToString()).Minute%> - <%=checkout%></td>
        <td><%=hour%> giờ <%=minute%> phút</td>
    </tr>
    <%}%>

    <%} %>
</body>
</html>
